/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：ModelEventHandlerRemoveChild.js<br>
 * 日期：2016年9月20日<br>
 * 作者: 王志远<br>
 * 功能：模型事件处理器<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.ModelEventHandlerRemoveChild<br>
 * 类型功能：模型事件请求<br>
 */
Rainier.UI.V2.ModelEventHandlerRemoveChild = Rainier.UI.V2.ModelEventHandler.extend({
	
	NAME : "Rainier.UI.V2.ModelEventHandlerRemoveChild",
	
	/**
	 * 函数名称：Rainier.UI.V2.ModelEventHandlerRemoveChild.init<br>
	 * 函数功能：构造函数<br>
	 * 参数输入：request:Rainier.UI.V2.ModelEventRequest
	 * 异常信息：无<br>
	 */
	init : function(request){
		this._super(request);
		
		var event 	  = request.event;
		this.newValue = event.getNewValue();

		this.parentEditPart  = request.editPart;
		this.parentFigure    = this.parentEditPart.getFigure();
		this.parentModel	 = this.parentEditPart.getModel();
		this.removedModel    = this.newValue.model;
		this.removedConntions= this.newValue.connections;
		this.removedPorts	 = this.newValue.ports;
	},
	
    /**
	 * 函数名称：draw2d.command.Command.canExecute<br>
	 * 函数功能：是否能够执行命令<br>
	 * 函数返回：Boolean<br>
	 * 异常信息：无<br>
	 */
    canExecute:function(){
      return true;
    },
    
    /**
	 * 函数名称：draw2d.command.Command.execute<br>
	 * 函数功能：执行命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    execute:function(){
    	this.parentEditPart.refreshChildren();
    },
    
    /**
	 * 函数名称：draw2d.command.Command.undo<br>
	 * 函数功能：撤销命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    undo:function(){
		if(this.removedModel instanceof Rainier.UI.V2.ConnectionModel){
			var parentModel = this.removedModel.getParent();
			if(parentModel){
				this.removedModel.attachSourcePort(this.removedModel.getSourcePort());
				this.removedModel.getSourcePort().attachOutgoing(this.removedModel);
				
				this.removedModel.attachTargetPort(this.removedModel.getTargetPort());
				this.removedModel.getTargetPort().attachIncoming(this.removedModel);
				parentModel.addConnection(this.removedModel);
			}
			this.parentPart.refreshChildren();
			return;
		}else{
			this.ports.each(function(j,c){
				this.removedModel.addPort(c);
			});
			this.parentModel.addChildModel(this.removedModel);
		}
		this.removedConntions.each(function(index,conn){
			var parentModel = conn.parentModel;
			var connection = conn.connection;
			if(parentModel){
		    	
				connection.attachSourcePort(conn.sourceport);
				conn.sourceport.attachOutgoing(connection);
				
				connection.attachTargetPort(conn.targetport);
				conn.targetport.attachIncoming(connection);

				parentModel.addConnection(conn.connection);
			}
		});
		this.parentPart.refreshChildren();
    },
    
    /**
	 * 函数名称：draw2d.command.Command.redo<br>
	 * 函数功能：重做命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    redo:function(){
    	var model = this.removedModel;
    	if(model instanceof Rainier.UI.V2.CompositeModel){
    		this.parentModel.removeChildModel(model);
    	}
    	else if(model instanceof Rainier.UI.V2.ConnectionModel){
    		this.parentModel.removeConnection(model);
    	}
    	else if(model instanceof Rainier.UI.V2.InputPortModel ||
    			model instanceof Rainier.UI.V2.OutputPortModel ||
    			model instanceof Rainier.UI.V2.HybridPortModel ){
    		this.parentModel.removePort(model);
    	}
    	this.parentPart.refreshChildren();
    }
});
